import {Injectable} from '@angular/core';
import {InterfaceProvider} from "@providers/interface";
import {StorageProvider} from "@providers/storage";
import {PagingProvider} from "@providers/paging";
import {HttpProvider} from "@providers/http";

/*
  Generated class for the WorkerProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class WorkerProvider {

  constructor(private ifp: InterfaceProvider,
              private paging: PagingProvider,
              private storage: StorageProvider,
              private http: HttpProvider) {

  }

  /**
   * 开工
   * @param partyId
   * @param sfzx
   * @returns {Promise<any>}
   */
  switchIsWorking(partyId, sfzx): Promise<any> {
    return this.ifp.post({
      interfaceId: "csm.som.jcdy.qcfwtools.fwryqd.update",
      data: {partyId: partyId, SFZX: sfzx}
    }).toPromise()
  }

  /**
   * 完工
   * @param item
   * * @returns {Promise<any>}
   */
  wandan(item): Promise<any> {
    return this.ifp.post({
      interfaceId: "csm.som.ext.cssOrder.sign",
      data: item || {},
    }).toPromise()
  }

  /**
   * 查询
   * @param item
   * @param pagingConfig
   * @returns {Promise<any>}
   */
  selectSOrder(item, pagingConfig?): Promise<any> {
    return this.paging.get({
      interfaceId: "csm.som.cssOrderquery.selectSOrder",
      data: item || {}
    }, pagingConfig).toPromise()
  }

  /**
   * 查询单据类型
   *
   * @param item
   * @param pagingConfig
   */
  getDjlx(item): Promise<any> {
    return this.ifp.post({
      interfaceId: "csm.css.jcdy.qcfwquery.getDjlx",
      data: item || {}
    }).toPromise()
  }

  /**
   * 查询外部分销送货单送货费用
   *
   * @param item
   * @param pagingConfig
   */
  getSHFY(item): Promise<any> {
    return this.http.post(
      "/query/api.do",
      {
        "response_compression": "gzip",
        "query_extend_access": true,
        "query_datasource": "scm",
        "response_data_format": "json",
        "response_page_size": "5",
        "response_speed_priority": true,
        "response_page_num": "1",
        "query_bean": "com.jlsoft.fwpt.qcfw.query.QuerySHFY",
        "query_inputdata": JSON.stringify({"DH": item.DH, "KHSF": item.KHSF, "STATUSID": item.STATUSID})
      }).toPromise()
  }

  /**
   * 接单
   * @param item
   * * @returns {Promise<any>}
   */
  dispatch(item): Promise<any> {
    let userInfo = this.storage.read("userInfo");
    return this.ifp.post({
      interfaceId: "csm.som.ext.cssOrder.dispatch",
      data: ({
        id: item.ID,
        partyId: userInfo['PARTYID'],
        worker_name: userInfo['USERNAME'],
        dispatcher: userInfo['USERNAME'],
        type: 'fw'
      })
    }).toPromise()
  }

  /**
   * 查询服务人员
   * @param item
   * * @returns {Promise<any>}
   */
  searchFWRY(item): Promise<any> {
    return this.ifp.post({
      interfaceId: "csm.css.jcdy.qcfwquery.fwry",
      data: item || {},
    }).toPromise()
  }

  /**
   * 确认收货
   */
  confirmReceipt(item): Promise<any> {
    return this.ifp.post({
      interfaceId: "css.parts.getZCD",
      data: item || {},
    }).toPromise()
  }

  /**
   * 获取部件信息
   */
  getPartsInfo(item, pagingConfig?): Promise<any> {
    return this.paging.get({
      interfaceId: "css.parts.query",
      data: item || {},
    }, pagingConfig).toPromise()
  }

  /**
   * 获取单据数量
   */
  getOrdersNum(item): Promise<any> {
    return this.ifp.post({
      interfaceId: "csm.css.getgdtj",
      data: item || {},
    }).toPromise()
  }

  /**
   * 物料申请
   * @param item
   * @returns {Promise<any>}
   */
  insertParts(item): Promise<any> {
    return this.ifp.post({
      interfaceId: "css.parts.insert",
      data: item || {},
    }).toPromise()
  }

  /**
   * 获取辅材
   * @param item
   * @returns {Promise<any>}
   */
  getFCFLSPFC(item): Promise<any> {
    return this.ifp.post({
      interfaceId: "csm.css.getFCFLSPFC",
      data: item || {},
    }).toPromise()
  }

  /**
   * 获取师傅口袋仓
   * @param item
   * @returns {Promise<any>}
   */
  getSFKDC(item): Promise<any> {
    return this.ifp.post({
      interfaceId: "csm.css.jcdy.qcfwquery.selectSFKDC",
      data: item || {},
    }).toPromise()
  }


  /**
   * 获取辅材分类
   * @param item
   * @returns {Promise<any>}
   */
  getFCFL(item): Promise<any> {
    return this.ifp.post({
      interfaceId: "csm.css.getFCFL",
      data: item || {},
    }).toPromise()
  }

  /**
   * 获取固定费用
   * @param item
   * @returns {Promise<any>}
   */
  getFCFLSP(item): Promise<any> {
    return this.ifp.post({
      interfaceId: "csm.css.getFCFLSP",
      data: item || {},
    }).toPromise()
  }

  /**
   * 查询挂单选项接口
   * @param item
   */
  selectGuaDan(item): Promise<any> {
    return this.ifp.post({
      interfaceId: "csm.css.jcdy.qcfwquery.selectGuaDan",
      data: item || {},
    }).toPromise()
  }

  /**
   * 挂单接口
   * @param item
   */
  makeGuaDan(item): Promise<any> {
    return this.ifp.post({
      interfaceId: "csm.css.jcdy.qcfwdefjk.makeGuaDan",
      data: item || {},
    }).toPromise()
  }

  /**
   * 结束挂单
   * @param item
   */
  makeGuaDanDown(item): Promise<any> {
    return this.ifp.post({
      interfaceId: "csm.css.jcdy.qcfwdefjk.makeGuaDanDown",
      data: item || {},
    }).toPromise()
  }

  /**
   * 获取控制内码是否必填、条码是否存在的参数
   */
  getNJMSFBT(item?): Promise<any> {
    return this.ifp.post({
      interfaceId: "csm.som.cssOrderquery.getNJMSFBT",
      data: item || {},
    }).toPromise()
  }

  /**
   * 获取配送失败原因
   */
  getPSSBYY(item): Promise<any> {
    return this.ifp.post({
      interfaceId: "csm.som.cssOrderquery.selectReason",
      data: item || {},
    }).toPromise()
  }

  /**
   * 获取完工记录
   * @param item
   * @param pagingConfig 分页配置
   */
  getProductNumSkip(item, pagingConfig): Promise<any> {
    return this.paging.get({
      interfaceId: "csm.css.getProductNumSkip",
      data: item || {}
    }, pagingConfig).toPromise()
  }

  /**
   * 获取完工记录详情
   * @param item
   */
  querywgxxSJ(item): Promise<any> {
    return this.ifp.post({
      interfaceId: "csm.css.jcdy.qcfwquery.querywgxxSJ",
      data: item || {}
    }).toPromise()
  }

  /**
   * //1、获取安装总数/预计提成//2、获取完工记录收费记录的数量
   * @param item
   */
  getSumNum(item?): Promise<any> {
    return this.ifp.post({
      interfaceId: "csm.css.getSumNum",
      data: item || {}
    }).toPromise()
  }

  /**
   * 获取故障原因
   * @param item
   */
  getFaultCause(item?): Promise<any> {
    return this.ifp.post({
      interfaceId: "csm.css.getgzyy",
      data: {}
    }).toPromise()
  }

  /**
   *获取故障原因对应的维修措施
   */
  getMaintenancemMeasures(faultCause): Promise<any> {
    return this.ifp.post({
      interfaceId: "csm.css.getwxcs",
      data: ({GZYY: faultCause})
    }).toPromise()
  }


  /**
   * 获取结算对象
   */
  getSettlementObjects(GSXX01, LX?): Promise<any> {
    return this.ifp.post({
      interfaceId: "csm.css.jcdy.qcfwquery.wldw",
      data: ({LX: LX, SSGSXX01: GSXX01})
    }).toPromise()
  }

  /**
   * 获取销售方式
   */
  getSalesMethods(): Promise<any> {
    return this.ifp.post({
      interfaceId: "csm.css.jcdy.qcfwquery.selectenum",
      data: ({TYPE: "css_flfs"})
    }).toPromise()
  }

  /**
   * 地理编码
   */
  geocode(url): Promise<any[]> {
    return this.http.get(url).toPromise();
  }

  /**
   * 拣货
   * @param ID
   */
  pick(ID): Promise<any> {
    return this.http.post('/Inbound/invoke.do', {
      interfaceId: 'csm.som.cssOrder.pick',
      data: JSON.stringify({"ID": ID})
    }).toPromise();
  }

  /**
   * 工资详情
   * @param item
   * @param pagingConfig
   */
  getSfCost(item, pagingConfig?): Promise<any> {
    return this.ifp.post({
      interfaceId: "csm.css.pay.getSfCost",
      data: item || {}
    }).toPromise()
  }
}
